package com.muyaxuan.mail.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.muyaxuan.mail.common.PageModel;
import com.muyaxuan.mail.dao.OrderDao;
import com.muyaxuan.mail.dao.OrderItemDao;
import com.muyaxuan.mail.dao.UserDao;
import com.muyaxuan.mail.dto.Order;
import com.muyaxuan.mail.dto.OrderItem;
import com.muyaxuan.mail.dto.User;
@Controller
@RequestMapping("/admin/order")
public class OrderController {
	PageModel pageModel = null;
    OrderDao orderDao = null;
    OrderItemDao orderItemDao = null;
    User user = null;
    UserDao userDao = null;
    Map<String,Object> map = null;
    
    /**
     * 分页查询订单 
     * @param orderCode 订单编号
     * @param name 用户姓名
     * @param pageIndex 当前页
     * @param data response数据
     * @return 视图资源
     */
	@RequestMapping("/orderList")
	public String showOrderList(String orderCode,String name,@RequestParam(name="pageIndex",defaultValue="0")int pageIndex,Model data){
		
		map = new HashMap<>();
		orderDao = new OrderDao();
		
		
		if(name!=null&&!"".equals(name)){
		userDao = new UserDao();
		user = new User();
		user = userDao.getUserByName(name);
		}
		pageModel = new PageModel();
		pageModel.setPageIndex(pageIndex);
		pageModel.setRecordCount(orderDao.count(user,orderCode));
		
		map.put("user", user);
		map.put("orderCode", orderCode);
		map.put("pageModel", pageModel);
		
		List<Order> orderList = orderDao.getOrderByPage(map);
		
		data.addAttribute("orderList", orderList);
		data.addAttribute("pageModel", pageModel);

		return "order/orderList";
	}
	@RequestMapping("/showDetail")
	public String showDetail(int id,Model data){
		
		orderItemDao = new OrderItemDao();
		//查询 订单详情
		List<OrderItem> orderItemList = orderItemDao.getOrderItemById(id);
		data.addAttribute("orderItemList", orderItemList);
		
		return "order/orderItem";
	}
	/**
	 * 删除订单
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(int id){
		
		orderDao = new OrderDao();	
		orderItemDao = new OrderItemDao();
		orderItemDao.delete(id);
		orderDao.delete(id);
		
		return "redirect:/admin/order/orderList";
	}
	
	
	
	
	
	

}
